use specs::{Join, ReadStorage, System, Write};
use std::collections::HashMap;

use crate::{
    components::{Box, BoxSpot, Position},
    resources::{Gameplay, GameplayState},
};

pub struct GameplayStateSystem {}   

impl<'a> System<'a> for GameplayStateSystem{
    type SystemData=(
        Write<'a,Gameplay>,
        ReadStorage<'a,Position>,
        ReadStorage<'a, Box>,
        ReadStorage<'a, BoxSpot>,
    );
    fn run(&mut self,data:Self::SystemData){
        let (mut gameplay_state,positions,boxes,box_spots)=data;
        let boxes_by_position=(&positions,&boxes).join()
        .map(|t|((t.0.x,t.0.y),t.1))
        .collect::<HashMap<_,_>>();

        for (box_spot, position) in (&box_spots, &positions).join() {
            if let Some(the_box) = boxes_by_position.get(&(position.x, position.y)) {
                //需要实现 PartialEq 才能 == 比较
                if the_box.colour == box_spot.colour {
                    // continue  所有的box 与 box_spot 都要颜色匹配 
                } else {
                    // return, haven't won yet
                    return;
                }
            } else {
                gameplay_state.state = GameplayState::Playing;
                return;
            }
        }

        gameplay_state.state = GameplayState::Won;
    }
}